#!/bin/sh

# %CopyrightBegin%
#
# SPDX-License-Identifier: Apache-2.0
#
# Copyright Ericsson AB 2025. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# %CopyrightEnd%

OUTPUT_FILE=$1

if command -v git 2>&1 >/dev/null &&
    test -d $ERL_TOP/.git -o -f $ERL_TOP/.git
then
    VSN=`git describe --match "OTP-[0-9]*" HEAD`
    case "$VSN" in
	OTP-*-g*) 
	    VSN=`echo $VSN | sed -e 's/.*-g\\(.*\\)/\\1/g'` ;;
	*)  VSN="na" ;;
    esac
else
    VSN="na"
fi


# Only update the file if there has been a change to
# the version number.
if test -r $OUTPUT_FILE
then
	VC=`sed -n -e 's/^.*"\\\\"\\(.*\\)\\\\"".*/\\1/p' < $OUTPUT_FILE`
else
	VC=unset
fi

if test "$VSN" != "$VC"
then
    echo "# Automatically generated by $0 - DO NOT EDIT." > $OUTPUT_FILE
    if test "$VSN" = "na"
    then
	echo "# GIT_VSN=-DERLANG_GIT_VERSION=\"\\\"$VSN\\\"\"" >> $OUTPUT_FILE
    else
	echo "GIT_VSN=-DERLANG_GIT_VERSION=\"\\\"$VSN\\\"\"" >> $OUTPUT_FILE
    fi
    exit 0
fi
exit 1
